<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/22 0022
 * Time: 14:58
 */

namespace app\finance\controller;


use think\Db;

class UserController extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }

    /**
     *创建用户
     */
    public function addUser()
    {
        echo $this->fetch();
    }

    /**
     * 保存创建用户数据
     * @return mixed
     */
    public function ajaxAddUser()
    {
        if(!empty($_POST)){
            $addUserData = $this->request->post('addUserData');
            unset($addUserData['pass_word_affirm']);
            $isExist = Db::table('user_info')->where('user_name', '=', $addUserData['user_name'])->find();
            if(!empty($isExist))return $this->ajaxFail(0, '该用户已经存在，请确认');
            $addResult = Db::table('user_info')->data($addUserData)->insert();
            if($addResult) return $this->ajaxSuccess();
        }
        return $this->ajaxFail();
    }

    /**
     *编辑用户
     */
    public function editUser()
    {
        $userId = $this->request->get('user_id');
        if(!is_numeric($userId))return $this->ajaxFail(0,'参数错误');
        $userInfo = Db::table('user_info')->where('id', '=', $userId)->find();
        if(empty($userInfo)) return '该用户已经不存在';
        $this->assign('userInfo', $userInfo);
        echo  $this->fetch();
    }
    /**
     * 保存创建用户数据
     * @return mixed
     */
    public function ajaxEditUser()
    {
        if(!empty($_POST)){
            $addUserData = $this->request->post('UserData');
            unset($addUserData['pass_word_affirm']);
            $isExist = Db::table('user_info')
                ->where('user_name', '=', $addUserData['user_name'])
                ->where('id', 'neq', $addUserData['id'])
                ->find();
            if(!empty($isExist))return $this->ajaxFail(0, '该用户已经存在，请确认');

            $Result = Db::table('user_info')->data($addUserData)->update();
            if($Result) return $this->ajaxSuccess();
        }
        return $this->ajaxFail();
    }

    /**
     * @return array
     * user search
     */
    public function ajaxUserChaXun()
    {
        $map = array();
        //if(session('user_name') != 'admin') return $data = array('code' => 0, 'msg' => '', 'count' => 0, 'data' => array());
        if(session('user_name') != 'admin'){
            $map[] = ['user_name','=', session('user_name')];
            $array = Db::table('user_info')->where($map)->order('id','desc')->select();
            $count = Db::table('user_info')->where($map)->count();
            $data = array('code' => 0, 'msg' => '', 'count' => $count, 'data' => $array);
            return $data;
        }
        if(!empty($_GET) and (isset($_GET['name']) or isset($_GET['card_num']))){
            if(!empty($_GET['name'])) $map[] = ['user_name','=',$_GET['name']];
            if(!empty($_GET['card_num'])) $map[] = array('card_num', '=',$_GET['card_num']);

            $array = Db::table('user_info')->where($map)->order('id','desc')->select();
            $count = Db::table('user_info')->where($map)->count();
            $data = array('code' => 0, 'msg' => '', 'count' => $count, 'data' => $array);
            return $data;
        }
        $page = empty($_GET['page']) ? 1 :$_GET['page'];
        $limit = empty($_GET['limit']) ? 15 :$_GET['limit'];
        $array = Db::table('user_info')->where($map)->limit($page - 1, $limit)->order('id','desc')->select();
        $count = Db::table('user_info')->count();
        $data = array('code' => 0, 'msg' => '', 'count' => $count, 'data' => $array);
        return $data;
    }

    /**
     *删除工资发发放表
     *
     *
     *
     */
    public function delete()
    {
        if(!empty($_POST) and isset($_POST['checkIdS'])){
            $checkIdArray = explode(',', trim($_POST['checkIdS'], ','));
            //管理员不能删除
            foreach ($checkIdArray as $index => $item) {
                if($item == 1) unset($checkIdArray[$index]);
            }
            $delResult = Db::table('user_info')->delete($checkIdArray);
            if($delResult) return $this->ajaxSuccess();
        }
        return $this->ajaxFail();
    }
}